package com.smbms.controller.recruitment;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.smbms.pojo.recruitment.Rt_releasejop;
import com.smbms.pojo.system.Og_Department;
import com.smbms.pojo.system.Og_Organiza;
import com.smbms.pojo.system.Og_Position;
import com.smbms.pojo.system.Og_Positionclass;
import com.smbms.service.recruitmentservice.ResumeService.RmService;
import com.smbms.service.recruitmentservice.releasejop.releasejopService;
import com.smbms.service.systemservice.organziation.OrganziationService;
import com.smbms.service.systemservice.positionService.positionService;
import com.smbms.service.systemservice.userService.UserService;



@Controller
public class RTYController {
	@Autowired
	private RmService rs;
	@Autowired
	private releasejopService rjs;
	@Autowired
	private positionService jop;//职位
	@Autowired
	private UserService bu;//所属部门
	
	@Autowired
	private positionService posService;
	@Autowired
	private OrganziationService orgService;
	
	// 跳转到职位发布管理界面
	@RequestMapping("/releasejop.html")
	public String getReleasJop(HttpServletRequest request) {
		List<Og_Positionclass> rjList = rjs.getRj();
		request.setAttribute("rjList", rjList);
		return "recruitment/rt_releasejop";
	}

	// 职位发布管理界面展示数据
	@RequestMapping(value = "/reLeasejop.json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getReLeaseJop() {
		// 创建Map集合
		Map<String, Object> map = new HashMap<String, Object>();
		// 创建List集合存放从数据库里获取的数据
		List<Rt_releasejop> rlist = rjs.getReleasejop();
		// 创建List集合存放对象集合
		List<Rt_releasejop> alllisrt = new ArrayList<Rt_releasejop>();
		for (Rt_releasejop rjs : rlist) {
			alllisrt.add(new Rt_releasejop(rjs.getId(), rjs.getRt_position_code(), rjs.getRt_position_name(),
					rjs.getRt_p_classname(), rjs.getRt_oz_name(), rjs.getRecritmentNumber(), rjs.getRegistrantTiem(),
					rjs.getEndingDate()));
		}
		map.put("data", alllisrt);
		System.out.println(JSONArray.toJSONString(map));
		return JSONArray.toJSONString(map);
	}

	// 模糊查询
	@RequestMapping(value = "/reLeasejopbt.json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getReleaseJopNTE(Rt_releasejop rt) {
		Map<String, Object> map = new HashMap<String, Object>();
		Rt_releasejop rl = new Rt_releasejop(rt.getRt_position_name(), rt.getP_classnameId(), rt.getRegistrantTiem(),
				rt.getEndingDate());
		List<Rt_releasejop> rtList = rjs.getReleasejopNTE(rl);
		map.put("data", rtList);
		return JSONArray.toJSONString(map);
	}

	
		/*//显示职位信息
			//根据条件查询
		@RequestMapping(value ="selectjop.json",produces = "application/json;charset=utf-8")
		@ResponseBody
			public String getSelctJop(Og_Position ops) {
				Map<String, Object> map = new HashMap<String, Object>();
				List<Og_Position> list=jop.getPosition(ops);
				System.out.println(list.size());
				map.put("data", list);
				return  JSONArray.toJSONString(map);
			}
*/
		
		// 职位发布登记
				@RequestMapping(value = "/rt_releaseadd.html")
				public String getReleaseadd(HttpServletRequest request) {
					String menus = getTreeList();
					request.setAttribute("menus", menus);

					return "recruitment/rt_releaseadd";

				}
				// 职位发布登记保存
				@RequestMapping(value = "/rt_releaseaddsave.html")
				public String getReleaseaddSave(Rt_releasejop rj) {
					System.out.println("fkd");
					if(rj.getRt_p_classname()=="管理") {
						rj.setP_classnameId(1);
					}else {
						rj.setP_classnameId(2);
					}
					int result =rjs.getReleaseajopadd(rj);
					if(result>0) {
						return "redirect:releasejop.html";
					}
					return "recruitment/rt_releaseadd";

				}
				
	
	// 明细
	@RequestMapping(value = "rt_releaseselect.html/{id}")
	public String getreleaseselect(@PathVariable Integer id, HttpServletRequest request) {
		Rt_releasejop rel = rjs.getRelease(id);
		request.setAttribute("rel", rel);
		return "recruitment/rt_releaseselect";
	}
	
	
		//职位发布变更
			@RequestMapping(value = "rt_releaseUpd.html/{id}")
			public String getreleaUpd(@PathVariable Integer id, HttpServletRequest request) {
				
				String menus = getTreeList();
				request.setAttribute("menus", menus);
				Rt_releasejop rel = rjs.getRelease(id);
				request.setAttribute("rel", rel);
				
				return "recruitment/rt_releaseUpd";
			}
		
		//职位发布变更保存
			@RequestMapping(value = "rt_releaseUpdSave.html")
			public String getreleaUpdsave(Rt_releasejop rj, HttpServletRequest request) {
				rj.setModifytime(new Date());
				int result =rjs.getReleaseUpd(rj);
				if(result>0) {
					return "redirect:releasejop.html";
				}
				return "recruitment/rt_releaseUpd";
			}	
	

	//删除职位发布信息
	@RequestMapping(value = "/del.json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getDel(Integer id) {
		boolean flag=true;
		int result =rjs.getDel(id);
		HashMap<String, Object> map =new HashMap<String, Object>();
		if(result>0) {
			map.put("data", flag);
		}
		System.out.println(JSONArray.toJSONString(map));
		return JSONArray.toJSONString(map);
	}
	

	
		//查询弹出框职位列表
		@RequestMapping(value="/ReleseJopopen.json",produces = "application/json;charset=utf-8")
		@ResponseBody
		public String getReleseJopOpen(Og_Position pos,HttpServletRequest request) {
			List<Og_Department> depList = bu.getDepartmentList();
			request.setAttribute("depList", depList);
			Map<String, Object> map =new HashMap<String,Object>();
			List<Og_Position> posList = posService.getPosition(pos);
			map.put("data", posList);
			return JSONArray.toJSONString(map);
		}
		
		//获取部门列表数据
		public String getTreeList(){
			//取得数据
			List<HashMap<Object, Object>> resultMap = rjs.getText();
			System.out.println(resultMap);
			//定义一个Map集合 存储按指定顺序排列好的菜单
			Map<String, List<Map<Object,Object>>> temp = new HashMap<String, List<Map<Object,Object>>>();
			
			for (HashMap<Object,Object> map : resultMap) {
				//如果temp的键包含这个parentid
				if(temp.containsKey(map.get("parentid").toString())){
					//那么把所有相同parentid的数据全部添加到该parentid键下
					temp.get(map.get("parentid").toString()).add(map);
				}else{
					//初始化temp  第一次用
					List<Map<Object,Object>> list = new ArrayList<Map<Object,Object>>();
					list.add(map);
					temp.put(map.get("parentid").toString(), list);
				}
			}
			//定义一个完整菜单列表
			ArrayList<Map<Object,Object>> array = new ArrayList<Map<Object,Object>>();
			
			for (HashMap<Object, Object> hashMap : resultMap) {
				//如果temp中的键与当前id一致
				if(temp.containsKey(hashMap.get("id").toString())){
					//说明temp是当前id菜单的子菜单
					hashMap.put("nodes", temp.get(hashMap.get("id").toString()));
				}
				//遇到顶级菜单就添加进完整菜单列表
				if(hashMap.get("parentid").toString().equals("0")){
					array.add(hashMap);
				}
			}
			return JSONArray.toJSONString(array);
		}

	
	
}
